package Cantella::DJabberd::Store::ResultSet::LoggedMessage;

use base qw/DBIx::Class::ResultSet/;
use metaclass 'Reaction::Meta::Class';
use Moose;

# sub _coerce_order_by_clause {
#   my ($self, $key) = @_;
#   if($key =~ /^\s*(\w+)\s*$/){
#     my $coercions = $self->order_by_coercions;
#     $key = $1;
#     if(exists($coercions->{$key})){
#       if($coercions->{$key} =~ /^(?:(\w+)\.)?\w+$/){
#         return [ $coercions->{$key}, ($1 ? $1 : ())];
#       } else {
#         confess('Malformed order_by coercion: '.$coercions->{$key});
#       }
#     }
#   }
#   return [$key];
# }

# sub search {
#   my $self = shift;
#   my $attrs = {};
#   if( @_ > 1 && ref($_[$#_]) eq 'HASH'){
#     $attrs = pop(@_);
#     if( defined(my $order_by = delete $attrs->{order_by}) ){
#       $order_by = [ $order_by ] unless ref($order_by) eq 'ARRAY';
#       my @new_order;
#       my @joins;
#       for my $piece ( @$order_by ){
#         if(my $ref = ref($piece)){
#           if($ref eq 'HASH'){
#             for my $key ('-desc','-asc'){
#               if(exists $piece->{$key}){
#                 my $new_v = $self->_coerce_order_by_clause($piece->{$key});
#                 push(@joins, pop(@$new_v)) if(@$new_v > 1);
#                 $piece->{$key} = shift(@$new_v);
#               }
#             }
#           }
#         } else {
#           my @new_pieces;
#           for my $sub_piece (split(/\s*,\s*/, $piece)){
#             if( $sub_piece =~ /(\w+)(?:\s+(ASC|DESC))?\s*/){ #XXX ENOSAFE
#               my $order;
#               my $key = $1;
#               $order = $2 if $2;
#               my $new_v = $self->_coerce_order_by_clause($key);
#               push(@joins, pop(@$new_v)) if(@$new_v > 1);
#               push(@new_pieces, shift(@$new_v));
#             }
#             $piece = join(', ',@new_pieces);
#           }
#         }
#         push(@new_order, $piece);
#       }
#       $attrs->{order_by} = \@new_order if @new_order;
#       if(@joins){
#         my %pending = map {$_ => 1} @joins;
#         my @to_check()
#         for my $join_type ('prefetch','join'){
#           if( exists($attrs->{$join_type}) ){
#             my $others = delete $attrs->{$join_type};

#             if( my $ref_type = ref($other) ){
#               if( $ref_type eq 'ARRAY'){
#                 map {delete $pending{$_} } @$others;
#               } elsif( $ref_type eq 'HASH') {
#                 map { delete $pending{$_} } @{keys %$others};
#                 map { delete $pending{$_} } grep {$ref $_} @{values %$others};
#               } else {
#                 confess("Non supported ref type ${ref_type}' in ${join_type} clause");
#               }
#             } else {
#               delete $pending{$others};
#             }

#             $attrs->{$join_type} = $others;
#             last unless keys %pending;
#           }
#         }
#         @joins = keys %pending;
#         if( @joins ){
#           if( !exists($attrs->{prefetch}) ) {
#             $attrs->{prefetch} = [ @joins ];
#           } elsif( my $reftype =  ref($attrs->{prefetch}) ) {
#             if( $ref_type eq 'ARRAY' ) {
#               push(@{ $attrs->{prefetch} }, @joins);
#             } elsif( $ref_type eq 'HASH' ){
#               $attrs->{prefetch} = [ @joins, $attrs->{prefetch} ];
#             }
#           } else {
#             $attrs->{prefetch} = [ $attrs->{prefetch}, @joins ];
#           }
#         }
#       }
#     }
#   }
#   return $self->next::method($_[0] || {}, {});
# }

{
  my $boom = sub {confess "This method is not supported. This is an unalterable result class.";};
  for my $method (qw/update update_all update_or_create delete delete_all/){
    __PACKAGE__->meta->add_method($method, $boom);
  }
}

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

1;
